﻿@{
    List<Category> categoryList = ViewBag.CategoryList ?? new List<Category>();
}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">标题</label>
                    <div class="layui-input-inline">
                        <input type="text" name="Title" id="Title" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">类别</label>
                    <div class="layui-input-inline">
                        <select name="CategoryId">
                            <option value="">请选择</option>
                            @foreach (var item in categoryList)
                            {
                                <option value="@item.Id">@item.CategoryName</option>
                            }
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-contlist-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>查询
                    </button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <div class="layui-btn-group" style="padding-bottom: 10px;">
                <button class="layui-btn  layui-btn-sm mybtn layuiadmin-btn-list" data-type="batchdel"><i class="layui-icon layui-icon-delete"></i>批量删除</button>
                <button class="layui-btn  layui-btn-sm mybtn layuiadmin-btn-list" data-type="add"><i class="layui-icon layui-icon-add-1"></i>添加数据</button>
            </div>
            <div class="layui-btn-group" style="padding-bottom: 10px; float: right">
                <button class="layui-btn  layui-btn-sm mybtn layuiadmin-btn-list" data-type="selectexportexcel"><i class="layui-icon layui-icon-add-circle"></i>选择导出</button>
                <button class="layui-btn  layui-btn-sm mybtn layuiadmin-btn-list" data-type="queryexportexcel"><i class="layui-icon layui-icon-download-circle"></i>查询导出</button>
            </div>
            <table class="layui-hide" id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
            <script type="text/html" id="LAY-app-content-list-bar">
                @*<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail"><i class="layui-icon layui-icon-search"></i>查看</a>*@
                <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>


@section scripts
{
    <script src="~/content/lib/jquery/jquery-1.8.2.min.js"></script>
    <script>
        layui.config({
            base: '/Content/LayuiAdmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'table', 'laydate'],
            function() {
                var admin = layui.admin, table = layui.table, form = layui.form, laydate = layui.laydate;
                var searchwhere;
                //监听搜索
                form.on('submit(LAY-app-contlist-search)',
                    function(data) {
                        var field = data.field;
                        searchwhere = field;
                        //执行重载
                        table.reload('LAY-app-content-list',
                            {
                                where: field
                            });
                    });

                //数据绑定
                table.render({
                    elem: '#LAY-app-content-list',
                    url: '@(Url.Action("List"))',
                    method: 'POST',
                    cellMinWidth: 80,
                    page: false,
                    limit: 15,
                    limits: [10, 15, 20, 25, 30, 50, 100, 200],
                    request: {
                        pageName: 'page',
                        limitName:'pageSize'
                    },
                    response: {
                        countName: 'recordCount' //数据总数的字段名称，默认：count
                        ,statusName:'statusCode'
                    },
                    text: {
                        none: '暂无相关数据'
                    },
                    cols: [
                        [
                            { field: 'id', title: '序列', sort: false },
                            {field:'title',title:'标题',sort:false},
                            { field: 'categoryName', title: '类别', sort: false },
                            { width: 300, align: 'center', fixed: 'right', toolbar: '#LAY-app-content-list-bar' }
                        ]
                    ]
                });

                var $ = layui.$,
                    active = {

                        add: function() {
                          var l=  layer.open({
                                type: 2,
                                title: '添加数据',
                                content: '@(Url.Action("Edit"))',
                                maxmin: true,
                                area: ['600px', '550px'],
                                btn: ['确定', '取消'],
                                yes: function(index, layero) {
                                    //点击确认触发 iframe 内容中的按钮提交
                                    var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
                                    submit.click();
                                }
                            });
                            layer.full(l);
                        }
                    };

                $('.layui-btn.layuiadmin-btn-list').on('click',
                    function() {
                        var type = $(this).data('type');
                        active[type] ? active[type].call(this) : '';
                    });
                //监听工具条
                table.on('tool(LAY-app-content-list)',
                    function(obj) {
                        var data = obj.data;
                        if (obj.event === 'del') {
                            layer.confirm('真的删除行么',
                                function(index) {
                                    admin.req({
                                        url: '@(Url.Action("Delete"))',
                                        type: 'POST',
                                        data: { id: data.id },
                                        done: function(res) {
                                            if (res.statusCode === 0) {
                                                //obj.del();
                                                table.reload('LAY-app-content-list');
                                                layer.close(index);
                                                top.layer.msg(res.message, { offset: '15px', icon: 1 });
                                            } else {
                                                layer.close(index);
                                                top.layer.msg(res.message, { offset: '15px', icon: 5 });
                                            }
                                        }
                                    });
                                });
                        } else if (obj.event === 'edit') {
                         var l =  layer.open({
                                type: 2,
                                title: '编辑数据',
                                content: '@(Url.Action("Edit"))?id=' + data.id,
                                maxmin: true,
                                area: ['600px', '550px'],
                                btn: ['确定', '取消'],
                                yes: function(index, layero) {
                                    //点击确认触发 iframe 内容中的按钮提交
                                    var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
                                    submit.click();
                                }
                            });
                            layer.full(l);
                        }
                    });
            });
    </script>
}